<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/1
 * Time: 19:06
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Order as OrderModel;
use app\api\service\Order as OrderService;
use app\api\service\Token as TokenService;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\NewOrder;
use app\api\validate\PagingParameter;
use app\lib\enum\ScopeEnum;
use app\lib\Exception\OrderException;

class Order extends BaseController
{
    protected $beforeActionList = [
        'checkOnlyUserScope' => ['only' => 'placeOrder'],
        'checkSuperScope' => ['only' => 'pageOrder'],
        'checkBaseScope' =>  ['only' => 'getDetail, getList']
    ];


    /*
     * 检查参数
     * 库存量
     * 库存够  下单
     * 支付前  再次检查库存
     * 支付成功
     * 检查库存 库存够  数据库更新库存
     * 库存不够 支付失败
     * */

    public function placeOrder()
    {
        (new NewOrder())->toCheck();
        // 获取用户uid
        $uid = TokenService::getCurrentUid();
        // 获取用户下单商品
        $oBooks = input('post.books/a');
        $orderType = input('post.type');

        // $orderType = input('post.type');
        // 下单
        $orderService = new OrderService();
        $orderState = $orderService->place($oBooks, $orderType, $uid);

        return $orderState;
    }

    // 看所有订单
    public function pageOrder($size=15, $page=1, $type = 'buyOrder'){
        (new PagingParameter())->toCheck();
        $pagination = OrderModel::getOrders($size, $page, $type);
        if($pagination->isEmpty()){
            return [
                'data'=> [],
                'type' => $type
                // 'current_page'=>$pagination->currentPage()
            ];
        }


        $pagination->hidden(['prepay_id']);

        $data = $pagination->toArray();
        return [
            'data'=> $data,
            'type' => $type
            // 'current_page'=>$pagination->currentPage()
        ];
    }

    public function getDetail($id){
        (new IDMustBePositiveInt())->toCheck();

        $scope = TokenService::getTokenVar('scope');

        // super权限可以查看
        // 用户权限只能查看自己的
        if($scope < ScopeEnum::Super){
            OrderService::isLegalOperate($id);
        }

        $result = OrderModel::getOrderDetail($id);
        if(!$result){
            throw new OrderException();
        }
        return $result->hidden(['prepay_id']);
    }

    // 某用户的订单
    public function getList($size = 12, $page = 1 ,$type = 'buyOrder'){
        (new PagingParameter())->toCheck();
        $uid = TokenService::getCurrentUid();
        $pagination = OrderModel::getOrderList($uid, $size, $page, $type);
        if($pagination->isEmpty()){
            return [
                'data'=> [],
                'current_page'=>$pagination->currentPage(),
                'type' => $type
            ];
        }

        $pagination->hidden(['snap_items','snap_address','prepay_id']);
        $data = $pagination->toArray();
        return [
            'data'=> $data,
            'current_page'=>$pagination->currentPage(),
            'type' => $type
        ];
    }
}